Stupid HTML forms question

Sampy

You can't take the sky from me...
Posted by Sampy // Sun, Feb 19, 2006 4:52 PM

I'm writing an app to automate a form query that I need to run over and over (getting data for the wife's thesis from the EPA) and I've encountered this in the HTML:

<INPUT TYPE="hidden" NAME="ft" VALUE="1">
<INPUT TYPE="hidden" NAME="ft" VALUE="2">

What is the value of "ft" in this scenario? 1? 2? 1;2?

Thanks for your help.
  Tek7
 
 
  Sun, Feb 19 2006 5:07 PM

Well, document.getElementsByName("ft")[0].value will = 1 and document.getElementsByName("ft")[0].value will = 2
on a post it will be 2 however.



  kidzi
 
 
  Sun, Feb 19 2006 5:10 PM
Sampy wrote:
I'm writing an app to automate a form query that I need to run over and over (getting data for the wife's thesis from the EPA) and I've encountered this in the HTML:

<INPUT TYPE="hidden" NAME="ft" VALUE="1">
<INPUT TYPE="hidden" NAME="ft" VALUE="2">

What is the value of "ft" in this scenario? 1? 2? 1;2?

Thanks for your help.


The raw value is "1,2"
However as the other poster said, it can be indexed.


  Sven Groot
 
 
  Sun, Feb 19 2006 5:38 PM
Both values will be sent to the server, so the post body will contain "ft=1&ft=2". ASP.NET will combine these values separated by a comma, so Request.Form("ft") will return "1,2". Other server technologies (PHP, JSP, etc.) might do differently.

  Maurits
  Hold ∞ in the palm of your hand [B̲̅l̲̅a̲̅k̲̅e̲̅]
 
  Sun, Feb 19 2006 11:07 PM

This is more usually seen with arrays of checkboxes, rather than hidden inputs... Sven is correct.

There are various ways to experiment with the interplay of type="..." and value="..." with different name="..."

The simplest method is to create a static HTML file that does a GET request to itself... then you can read off what the browser does from the query string.  More complicated methods involve browser extensions like Fiddler (for IE) or LiveHTTPHeaders (for Firefox)

There are some tricky byways... for example
* checkboxes have a default value of "on"
* disabled inputs have no presence in the query string (or form body) at all
* if there are multiple submit buttons with name/values, only the one clicked contributes to the request
* if you press Enter in an <input type="textbox">, IE will "click" the first <input type="submit"> for you (sending its name/value pair), but Firefox will just submit the form without any type="submit" name/value pairs1
* <input type="image"> has the same behavior has type="submit"

EDIT:
1This is responsible for a bug on Channel9... if you edit your profile settings in Firefox, and submit the form by pressing Enter in a text field, the form submits but none of the changes are applied.



  sbc
  Get Firefox (tabs, themes, extensions, standards)
 
  Mon, Feb 20 2006 6:39 AM
Maurits wrote:
* disabled inputs have no presence in the query string (or form body) at all

* if there are multiple submit buttons with name/values, only the one clicked contributes to the request

I think these are by design - if you disable something, it means you don't want it (so it is not submitted), you use 'readonly' if you don't want it to be edited. If all the submit buttons values on a page were submitted, ASP.NET would not even work (as I assume it uses the ID of the button clicked to determine the action server side). I find the DefaultButtons Control helps when it comes to multiple submit buttons and text inputs and the ENTER key.

First time I've seen an MS developer ask a question from the community (I would have thought there would be internal only help forums). At least it wasn't a VB question (I would be really worried if Sampy had a problem with some VB code).


  Sven Groot
 
 
  Mon, Feb 20 2006 6:54 AM
sbc wrote:
Maurits wrote:
* disabled inputs have no presence in the query string (or form body) at all

* if there are multiple submit buttons with name/values, only the one clicked contributes to the request

I think these are by design


Indeed. The HTML spec says only "successful controls" should be included in the form data. It defines for each type of control when it is "successful". A disabled input, unchecked checkbox or unclicked button is not "successful" thus not included in the form data. ASP.NET checks the presence of the submit button value in the form data to determine which Click event handler to launch, which is why it fails if none is sent.

Maurits wrote:
* checkboxes have a default value of "on"

Yes and no. In every browser I know of, that's indeed the case. But per the spec, they have no default value. The spec says the following about the value attribute of input: " It is optional except when the type attribute has the value "radio" or "checkbox". "

So for an <input type="checkbox">, you must include a value attribute for it to be valid HTML. Note that the asp:CheckBox control does not generate a value attribute, so it generates illegal HTML in this case, even in ASP.NET 2.0.

The problem is that this constraint (optional except for certain values of type) cannot be expressed in a DTD, and the W3C HTML Validator is limited to DTD validation so it cannot check this, and will say an input type="checkbox" without value is correct even though it isn't.

FWIW, I bugged this against ASP.NET, and against the validator.



  Sampy
  You can't take the sky from me...
 
  Mon, Feb 20 2006 2:22 PM
Thanks for the help!

Submitting it as ft=1&ft=2&... worked just fine for me. My screenscraper/auto-downloader worked great and saved my wife hours of annoying work :)

RE: asking a question here rather than internally
It was the weekend and I know there are smart web guys here but I'd have to hunt and pray to find the internal web support list. Path of least resistance!